﻿using System;
using MongoDB.Bson;
using MongoDB.Driver;
using FreeSql.DataAnnotations;
using Model;
using System.Collections.Generic;

namespace DAL
{
    public class User
    {
        //获取所有用户信息转为列表
        //应用场景：遍历检查User是否存在；
        public List<UserInfo> GetUser()
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<UserInfo> Info;
                Info = fsql.Select<UserInfo>()
                    .ToList();
                return Info;
            }
            catch
            {
                return null;
            }
        }

        //根据用户act获取其信息（用户名，密码，地址，真名，性别，电话）
        //应用场景：判断密码与身份；展示用户信息；
        public UserInfo GetUserByAct(string account)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<UserInfo> Info;
                Info = fsql.Select<UserInfo>()
                    .Where(a => a.account == account)
                    .ToList();
                return Info[0];
            }
            catch
            {
                return null;
            }
        }
        //插入新用户信息（用户名，密码）
        //应用场景：注册
        public int InsertUser(UserInfo user)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Insert(user)
                    .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }

        }
        //修改用户信息,oldAct为旧用户名（用户名，性别)
        public int UpdateUser(UserInfo user, string oldAct)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Update<UserInfo>()
                    .Set(a => a.account, user.account)
                    .Set(a => a.sex, user.sex)
                    .Where(a => a.account == oldAct)
                    .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }

        }
        //修改用户收货信息,oldAct为旧用户名（用户名，真名，收货地址，电话）
        public int UpdateUserAddr(UserInfo user, string oldAct)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Update<UserInfo>()
                    .Set(a => a.account, user.account)//修改收货地址是否能修改account？
                    .Set(a => a.name, user.name)
                    .Set(a => a.address, user.address)
                    .Set(a => a.tel, user.tel)
                    .Where(a => a.account == oldAct)
                    .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //修改用户密码,newpass为新用户密码（用户名，密码）
        public int UpdateUserPass(UserInfo user, string newpass)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Update<UserInfo>()
                    .Set(a => a.password, newpass)
                    .Where(a => a.account == user.account)
                    .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //删除用户信息
        public int DeleteUser(string account)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Delete<UserInfo>()
                   .Where(a => a.account == account)
                   .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //根据用户act获取头像
        public byte[] GetUserIcon(string account)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var collection = mongodb.GetCollection<BsonDocument>("UserIcon");
                var query = new QueryDocument { { "ID", account } };
                var result = collection.Find(query);
                byte[] img = (byte[])(result.ToList()[0]).GetValue("Img");
                return img;
            }
            catch
            {
                return null;
            }
        }
        //向MongoDB数据库中添加用户头像(初始化，默认最开始无头像的时候)
        public int InsertUserIcon(string account, byte[] newicon)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                BsonDocument doc = new BsonDocument();
                doc["ID"] = account;
                doc["Img"] = newicon;
                var collection = database.GetCollection<BsonDocument>("UserIcon");
                collection.InsertOneAsync(doc);
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //向MongoDB数据库中修改用户头像
        public int UpdateUserIcon(string account, byte[] newicon)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                var col = database.GetCollection<BsonDocument>("UserIcon");
                var query = new QueryDocument { { "ID", account } }; //定义获取“ID”值为“account”的查询条件
                //定义更新文档
                var update = new UpdateDocument { { "$set", new QueryDocument { { "Img", newicon } } } };
                //执行更新操作
                col.UpdateOneAsync(query, update);
                return 0;
            }
            catch
            {
                return -1;
            }

        }
        //从MongoDB数据库中删除用户头像
        public int DeleteUserIcon(string account)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                //获取集合
                var col = database.GetCollection<BsonDocument>("UserIcon");
                //定义获取“ID”值为account的查询条件
                var query = new QueryDocument { { "ID", account } };
                //执行删除操作
                col.DeleteMany(query);
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        public int UpdUserIconAct(string newact, string oldact)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                //获取集合
                var col = database.GetCollection<BsonDocument>("UserIcon");
                var query = new QueryDocument { { "ID", oldact } };
                var update = new UpdateDocument { { "$set", new QueryDocument { { "ID", newact } } } };
                col.UpdateOneAsync(query, update);

            }
            catch
            {
                return -1;
            }
            return 0;
        }
    }
}

